/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fp.miscs;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.util.Enumeration;
import java.util.Properties;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author iroiro
 */
public class Test_A {

    public static void main(String args[]) {
        try {
            
            Test_A.leProps();
        } catch (IOException ex) {
            Logger.getLogger(Test_A.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void leProps() throws IOException {
        Properties props = new Properties();
        //tenta ler o arquivo de propriedades dentro do pacote
        //FileOutputStream fos = new FileOutputStream("/org/fp/files/db.properties");
        //ou simplesmente 
        //props.load(new FileInputStream("org/fp/files/db.properties"));
        //nas duas opcoes acima o caminho em run time pode nao ser identicado se
        //o aplicativo nao estiver no classpath do sistema gerando uma excessao
        //(FileNotFoundException) por isso usaremos 
        //uma forma melhor para localizar caminho real (true path) da props.
        URL caminho = ClassLoader.getSystemResource("org/fp/files/db.properties");
        //notem que quando informado o contaner (jar) comencando com / o classloader
        //retorna null.
        if (caminho != null) {
            props.load(caminho.openStream());            
        }else return;

        props.list(System.out);
    }

    private static void criaProps() throws FileNotFoundException, IOException {
        Properties props = new Properties();
//        props.setProperty("Campo1", "VALOR1");
//        props.setProperty("Campo2", "VALOR2");
//        props.setProperty("Campo3", "VALOR3");
//        props.setProperty("Campo4.5.6", "VALOR4");
//        props.setProperty("Campo5", "VALOR5");
//        props.setProperty("Campo6", "VALOR6");
//        props.setProperty("Campo7", "VALOR7");
//        props.setProperty("Campo8", "VALOR8");
//        props.setProperty("Campo9", "VALOR9");
        props.put("Campo1", "VALOR1");
        props.put("Campo2", "VALOR2");
        props.put("Campo3", "VALOR3");
        props.put("Campo4.5.6", "VALOR4");
        props.put("Campo5", "VALOR5");
        props.put("Campo6", "VALOR6");
        props.put("Campo7", "VALOR7");
        props.put("Campo8", "VALOR8");
        props.put("Campo9", "VALOR9");
        FileOutputStream fos = new FileOutputStream("tst.xml");
        props.storeToXML(fos, "Nova propriedade para trabalho.");
        fos.close();
        FileInputStream fis = new FileInputStream("tst.properties");
        props.loadFromXML(fis);
        props.list(System.out);

    }

    private static void listaPropsSistema() {
        Properties prop = System.getProperties();
        Enumeration e = prop.propertyNames();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            System.out.println(key + " -- " + prop.getProperty(key));
        }
    }

    private void testaDB() {
        //        try {
//            //Test_A.listaPropsSistema();
//            Class.forName("com.mysql.jdbc.Driver");
//            DatabaseMetaData dmd;
//            Connection conn = DriverManager.getConnection(
//                    "jdbc:mysql://localhost:3306/caixaareia?user=iroiro&password=wolve");
//            dmd = conn.getMetaData();
//            System.out.println("" + dmd.getDatabaseProductName());
//            System.out.println("" + dmd.getDatabaseProductVersion());
//            System.out.println("" + dmd.getDriverName());
//            System.out.println("" + dmd.getDefaultTransactionIsolation());
//            conn.close();
//
//        } catch (SQLException ex) {
//            Logger.getLogger(Test_A.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (Exception exe) {
//            Logger.getLogger(Test_A.class.getName()).log(Level.SEVERE, null, exe);
//        }
        try {
            Class.forName("org.sqlite.JDBC");
            DatabaseMetaData dmd;
            Connection conn = DriverManager.getConnection("jdbc:sqlite:caixaareia.db");
            dmd = conn.getMetaData();
            System.out.println("" + dmd.getDatabaseProductName());
            System.out.println("" + dmd.getDatabaseProductVersion());
            System.out.println("" + dmd.getDriverName());
            System.out.println("" + dmd.getDefaultTransactionIsolation());
            System.out.println("" + conn.getWarnings());
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Test_A.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception exe) {
            Logger.getLogger(Test_A.class.getName()).log(Level.SEVERE, null, exe);
        }

    }
}
